<?php if(! defined('HAS_POWER') ) exit('无权访问!');
/**
==============================================================================
 * Class CreateModule GracePHP的模块自动创建
 * @author 肖永标
 * @date 2014-11-30
==============================================================================
 */
class CreateModule {

    /**
     * @var string $_controllerDir  控制器文件夹
     */
    private $_controllerDir;

    /**
     * @var string $_modelDir  模型文件夹
     */
    private $_modelDir;
    /**
     * @var string $_viewDir  视图文件夹
     */
    private $_viewDir;

    /**
     * @var string $_indexDir 测试控制器的对应的模板文件夹
     */
    private $_indexDir;

    /**
     * @var string $_componentDir 组件文件夹
     */
    private $_componentDir;

    /**
     * @var string $configDir  应用的配置文件夹
     */
    private $_configDir;

    /**
     * @var string $_logDir  日志文件类
     */
    private $_logDir;

    /**
     *  运行主函数
     */
    public function run()
    {
        $application = Config::getItem('application');
		
		if (!is_dir($application)) mkdir($application, 0777, true);
		
        if(!is_dir($application . MYAPP))
        {
            $this->_createDir($application . MYAPP);
        }
    }

    /** 创建模块必要文件夹
     * @param string $path 创建的应用文件夹路径
     */
    private function _createDir($path)
    {
        $this->_controllerDir = $path . 'controller';
        $this->_modelDir = $path . 'model';
        $this->_viewDir = $path . 'views';
        $this->_indexDir = $path . 'views/index';
        $this->_componentDir = $path . 'component';
        $this->_logDir = $path . 'log';
        $this->_configDir = $path . 'config';

        $dirArr = array($this->_controllerDir, $this->_modelDir,
            $this->_viewDir, $this->_indexDir, $this->_componentDir, $this->_logDir, $this->_configDir);

        // 创建模块文件夹
        mkdir($path, 0777, true);

        foreach ($dirArr as $k => $dir)
        {
            mkdir($dir, 0777, true);
        }
        $this->_createFile();
    }

    /**
     * 创建文件夹里面的测试文件
     */
    private function _createFile()
    {
        $testController = $this->_controllerDir . '/IndexController.class.php';
        $testView = $this->_indexDir . '/index.php';
        $config = $this->_configDir . '/config.php';
        $this->_fillContent($testController, $testView, $config);
    }

    /** 填充测试文件的内容
     * @param $testController string 测试Controller的文件夹位置
     * @param $testView string 测试Views的文件夹位置
     */
    private function _fillContent($testController, $testView, $config)
    {
        $crContent = '';
        $crContent .= "<?php if(! defined('HAS_POWER') ) exit('无权访问!');\r\n";
        $crContent .= "// 这是一个自动生成的测试类和方法，请自行修改或者删除\r\n";
        $crContent .= "class IndexController extends Controller { \r\n";
        $crContent .= "\t public function index() { \r\n";
        $crContent .= "\t\t ".'$this->render(\'index\');'." \r\n";
        $crContent .= "\t } \r\n";
        $crContent .= "} \r\n";

        $viewContent = <<<HTML
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>欢迎使用GracePHP</title>
</head>
<body>
<h1>:) hello，GracePHP~</h1>
</body>
</html>
HTML;

        $configContent = '';
        $configContent .= "<?php \r\n";
        $configContent .= "// 应用的配置文件项，可用数组对应参数覆盖框架配置\r\n";
        $configContent .= "return array(\r\n";
        $configContent .= ");";

        @file_put_contents($testController, $crContent);
        @file_put_contents($testView, $viewContent);
        @file_put_contents($config, $configContent);
    }
}